package cc.sunni.user.controller;

import cc.sunni.api.BaseController;
import cc.sunni.api.controller.user.AppUserMngControllerApi;
import cc.sunni.api.utils.PageUtils;
import cc.sunni.common.Constant;
import cc.sunni.common.enums.UserStatus;
import cc.sunni.common.utils.R;
import cc.sunni.common.utils.ResultEnum;
import cc.sunni.pojo.vo.PublisherVO;
import cc.sunni.user.service.AppUserMngService;
import cc.sunni.user.service.AppUserService;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class AppUserMngController extends BaseController implements AppUserMngControllerApi {
    @Autowired
    private AppUserMngService appUserMngService;
    @Autowired
    private AppUserService userService;

    @Override
    public R queryAll(String nickname, Integer status, Date startDate, Date endDate, Integer page, Integer pageSize) {
        PageUtils result = appUserMngService.queryAllUserList(nickname, status, startDate, endDate, page, pageSize);
        return R.ok(result);
    }

    @Override
    public R userDetail(String userId) {
        return R.ok(userService.getUserByUserId(userId));
    }

    @Override
    public R freezeUserOrNot(String userId, Integer doStatus) {
        if (!UserStatus.isUserStatusValid(doStatus)) {
            return R.en(ResultEnum.USER_STATUS_ERROR);
        }
        appUserMngService.freezeUserOrNot(userId, doStatus);
        // 刷新用户状态：删除用户会话，从而保障用户需要重新登录以后再来刷新他的会话状态
        redis.del(Constant.REDIS_USER_TOKEN + ":" + userId);
        redis.del(Constant.REDIS_USER_INFO + ":" + userId);
        return R.ok();
    }

    @Override
    public R queryAll(String userIds) {
        if (StringUtils.isBlank(userIds)) {
            return R.en(ResultEnum.USER_NOT_EXIST_ERROR);
        }
        List<String> userIdList = JSONUtil.toList(userIds, String.class);
        List<PublisherVO> userList = userService.getUserList(userIdList);
        return R.ok(userList);
    }
}
